package com.zsummer.zero.thread;

/**
 * synchronized�ؼ���
 * ....ʡ��
 * ȫ������ʵ����
 * @author zhouyujiang
 *
 */
public class ThreadDemo5 {
	
	private Object lock = new Object();
	
	public static void main(String[] args) {
		
		//run1();
		run2();
	}

	/**
	 * synchronized���ڷ�����
	 */
	private static void run2() {
		ThreadDemo5 threadDemo5 = new ThreadDemo5();
		Thread thread1 = new Thread(() -> {
			threadDemo5.syncMethod();
		}) ;
		Thread thread2 = new Thread(() -> {
			threadDemo5.syncMethod();
		}) ;
		thread1.start();
		thread2.start();
	}

	private synchronized void syncMethod() {
		for (int i = 0; i < 100; i++) {
			System.out.println(Thread.currentThread().getName() + " " + i);
		}
	}

	/**
	 * synchronized�����
	 */
	private static void run1() {
		ThreadDemo5 threadDemo5 = new ThreadDemo5();
		Thread thread1 = new Thread(() -> {
			synchronized (threadDemo5.lock) {
				for (int i = 0; i < 100; i++) {
					System.out.println(Thread.currentThread().getName() + " " + i);
				}
			}
		}) ;
		
		Thread thread2 = new Thread(() -> {
			synchronized (threadDemo5.lock) {
				for (int i = 0; i < 100; i++) {
					System.out.println(Thread.currentThread().getName() + " " + i);
				}
			}
		}) ;
		
		thread1.start();
		thread2.start();
	}

}
